<template>
  <div class="card">
    <div class="card-header" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filterFrom">
        <el-form-item>
          类别
          <el-select v-model="filterFrom.type" :loading="initTypeList">
            <el-option label="全部" value=""></el-option>
            <el-option v-for="option in typeList" :label="option.name" :value="option.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="fetchData">确定</el-button>
        </el-form-item>
        <div class="pull-right">
          <el-button type="primary" icon="plus" @click="handleAdd">增加资讯</el-button>
        </div>
      </el-form>
    </div>
    <div class="card-body card-padding">
      <el-table :data="tableData" style="width: 100%" v-loading="isLoading" border>
        <el-table-column type="expand">
          <template scope="props">
            <img :src="props.row.title_image" style="float: left;max-width:100px;height:75px;" />
            <div style="float: left;margin-left: 10px;width: calc(100% - 150px);">
              <dl class="dl-horizontal">
                <dt>摘要：</dt>
                <dd>{{props.row.summary}}</dd>
              </dl>
              <dl class="dl-horizontal">
                <dt>来源：</dt>
                <dd>{{props.row.source}}</dd>
              </dl>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="id" label="ID" width="70"></el-table-column>
        <el-table-column prop="type_name" label="类别" width="110"></el-table-column>
        <el-table-column prop="title" label="标题" width="300"></el-table-column>
        <el-table-column prop="publish_time" label="发布时间" width="250"></el-table-column>
        <el-table-column prop="view_count" label="浏览量" width="80"></el-table-column>
        <el-table-column label="是否失效" :formatter="disableFormatter" width="100"></el-table-column>
        <el-table-column label="操作">
          <template scope="scope">
            <el-button size="small" type="primary" @click="handleEdit(scope.row)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="table-pagination">
        <div style="float: left;margin-left: 20px;">
          <el-button size="small" type="text" @click="fetchData"><i class="zmdi zmdi-refresh" />&nbsp;&nbsp;重新加载</el-button>
        </div>
        <el-pagination :current-page="filterFrom.page_number" :page-sizes="[5, 10, 15, 20]" :page-size="filterFrom.page_size" layout="sizes, prev, pager, next, jumper,total"
          :total="totalSize" @size-change="handleSizeChange" @current-change="handlePageChange">
          </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
  import API from '../../api'

  var filterFrom = {
    start_time: '',
    end_time: '',
    page_number: 1,
    page_size: 10,
    type: '',
    enable: ''
  }

  export default {
    data: function () {
      return {
        typeList: [],
        initTypeList: true,
        isLoading: false,
        tableData: [],
        filterFrom,
        totalSize: 0
      }
    },
    created: function () {
      this.fetchData()
      this.initSelectType()
    },

    methods: {
      handleSizeChange: function (val) {
        this.filterFrom.page_size = val
        this.fetchData()
      },
      handlePageChange: function (val) {
        this.filterFrom.page_number = val
        this.fetchData()
      },

      handleAdd: function () {
        this.$router.push('/news/list/edit/0')
      },
      // 编辑
      handleEdit: function (SelectData) {
        this.$router.push('/news/list/info/' + SelectData.id)
      },

      fetchData: function () {
        this.isLoading = true
        API.newsList(this.filterFrom, (data) => {
          this.isLoading = false
          this.tableData = data.list
          this.totalSize = data.total_size
        })
      },
      initSelectType: function () {
        API.newsTypeList((data) => {
          this.initTypeList = false
          if (data) {
            this.typeList = data
          }
        })
      },
      disableFormatter: function (row) {
        return row.disable === 1 ? '是' : '否'
      },
      statusFormatter: function (row) {
        switch (row.admin_verify) {
          case 2: return '审核不通过'
          case 1: return '审核通过'
          default: return '未审核'
        }
      }
    }
  }

</script>

<style>
  .table-pagination {
    text-align: right;
    padding: 10px 0px;
    background: #eef1f6;
    border: 1px solid #dfe6ec;
    border-top: none
  }

  dt {
    width: 60px !important;
  }

  dd {
    margin-left: 65px !important;
  }
</style>
